package com.mofeng.myshop.until;

import com.mofeng.myshop.domain.vo.user.UserVo;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class JwtUtil {
    private static String key = "3866a78125581a861c4baef4b4142d8f";

    private static long ttl = Constants.Times.MONTH * 1000L;//默认存活时间 1小时

    public static String createToken(UserVo user, long ttl) {
        Map<String, Object> claims = createClaims(user);
        return createJWT(claims);
    }

    public static String createToken(UserVo user) {
        return createToken(user, ttl);
    }

    public static UserVo parseToken(String jwtStr) {
        Claims claims = null;
        try {
            claims = parseJWT(jwtStr);
            return parseClaims(claims);
        } catch (Exception e) {
            return null;
        }
    }

    private static Map<String, Object> createClaims(UserVo user) {//改成自己需要转换的类
        Map<String, Object> claims = new HashMap<>();
        claims.put("userName", user.getName());
        claims.put("image", user.getImageUrl());
        claims.put("id", user.getId());
        claims.put("powerNum", user.getPowerNumber());
        claims.put("powerName", user.getPowerName());
        claims.put("roleName", user.getRoleName());
//        claims.put("money", user.getMoney());
//        claims.put("amount", user.getAmount());
//        claims.put("sex", user.getSex());
        return claims;
    }

    private static UserVo parseClaims(Claims claims) {
        UserVo userVo = new UserVo();
        userVo.setName(claims.get("userName", String.class));
        userVo.setImageUrl(claims.get("image", String.class));
        userVo.setId(claims.get("id", String.class));
        userVo.setPowerNumber(claims.get("powerNum", Integer.class));
        userVo.setPowerName(claims.get("powerName", String.class));
        userVo.setRoleName(claims.get("roleName", String.class));
//        userVo.setMoney(claims.get("money", String.class));
//        userVo.setAmount(claims.get("amount", String.class));
//        userVo.setSex(claims.get("sex", String.class));
        return userVo;
    }

    /**
     * 创建JWT字符串
     *
     * @param claims 载荷内容
     * @param ttl    有效日期，秒 默认1小时
     */
    private static String createJWT(Map<String, Object> claims, long ttl) {
        JwtUtil.ttl = ttl * 1000;
        return createJWT(claims);
    }

    /**
     * @param claims 载荷
     * @return token
     */
    private static String createJWT(Map<String, Object> claims) {
        long nowMillis = System.currentTimeMillis();
        Date now = new Date(nowMillis);
        JwtBuilder builder = Jwts.builder()
                .setIssuedAt(now)
                .signWith(SignatureAlgorithm.HS256, key);
        if (claims != null) {
            builder.setClaims(claims);
        }
        if (ttl > 0) {
            builder.setExpiration(new Date(nowMillis + ttl));
        }
        return builder.compact();
    }

    /**
     * 创建Token
     *
     * @param key 关键字
     * @param ttl 秒值
     * @return
     */
    public static String createJWT(String key, long ttl) {
        long nowMillis = System.currentTimeMillis();
        Date now = new Date(nowMillis);
        JwtBuilder builder = Jwts.builder()
                .setId(key)
                .setIssuedAt(now)
                .signWith(SignatureAlgorithm.HS256, JwtUtil.key);
        if (ttl > 0) {
            ttl = ttl * 1000;
            builder.setExpiration(new Date(nowMillis + ttl));
        }
        return builder.compact();
    }

    /**
     * 解析字符串，过期则报错
     *
     * @param jwtStr
     * @return
     */
    private static Claims parseJWT(String jwtStr) {
        return Jwts.parser()
                .setSigningKey(key)
                .parseClaimsJws(jwtStr)
                .getBody();
    }

    public static void setTtl(long ttl) {
        JwtUtil.ttl = ttl;
    }
}
